import 'package:flutter/material.dart';
import '../../config.dart';
import '../../common/utils/i18n.dart';

/// 多文本组件,主要用于复杂的i18n文本，比如带变量的文本
class MyTexts extends StatelessWidget {
  final List<String> data; //文本内容
  //需要忽略I18n的文本index集合
  final Set<int> ignoreSet;
  final double? size;
  final Color? color;
  final FontWeight? weight;
  final TextAlign? textAlign;
  final bool? i18n;
  final TextScaler? textScaler;
  final double? letterSpacing;
  final int? maxLines;
  final TextOverflow? overflow;
  final bool? underline;
  final String? fontFamily;

  const MyTexts(
    this.data, {
    super.key,
    this.ignoreSet = const {},
    this.size,
    this.color,
    this.weight,
    this.textAlign,
    this.i18n,
    this.textScaler,
    this.letterSpacing,
    this.maxLines,
    this.overflow,
    this.underline,
    this.fontFamily,
  });

  @override
  Widget build(BuildContext context) {
    final sb = StringBuffer();
    for (int i = 0; i < data.length; i++) {
      if (ignoreSet.contains(i) || i18n == false) {
        sb.write(data[i]);
      } else {
        sb.write(data[i].i18n);
      }
    }
    final text = sb.toString();
    return MyText(
      text,
      key: key,
      size: size,
      color: color,
      weight: weight,
      textAlign: textAlign,
      i18n: false,
      textScaler: textScaler,
      letterSpacing: letterSpacing,
      maxLines: maxLines,
      overflow: overflow,
      underline: underline,
      fontFamily: fontFamily,
    );
  }
}

class MyText extends Text {
  MyText(
    String data, {
    Key? key,
    double? size,
    Color? color,
    FontWeight? weight,
    TextAlign? textAlign,
    bool? i18n,
    TextScaler? textScaler, // 字体缩放
    double? letterSpacing,
    int? maxLines,
    TextOverflow? overflow,
    bool? underline, // 是否下划线
    String? fontFamily,
  }) : super(
          (i18n ?? JmConfig.i18nEnable ? data.i18n : data),
          key: key,
          style: TextStyle(
              fontSize: size ?? JmConfig.defTextStyle.fontSize,
              color: color ?? JmConfig.defTextStyle.color,
              fontWeight: weight ?? JmConfig.defTextStyle.fontWeight,
              letterSpacing: letterSpacing,
              decoration: underline == true ? TextDecoration.underline : null,
              decorationColor: underline == true ? color : null,
              fontFamily: fontFamily),
          textAlign: textAlign,
          textScaler: textScaler,
          maxLines: maxLines,
          overflow: overflow,
        );

  factory MyText.sub(
    String data, {
    Key? key,
    double? size,
    Color? color,
    FontWeight? weight,
    TextAlign? textAlign,
    bool? i18n,
    TextScaler? textScaler,
    double? letterSpacing,
    int? maxLines,
    TextOverflow? overflow,
    bool? underline,
    String? fontFamily,
  }) {
    return MyText(
      data,
      key: key,
      size: size ?? JmConfig.sbuTextStyle.fontSize,
      color: color ?? JmConfig.sbuTextStyle.color,
      weight: weight,
      textAlign: textAlign,
      i18n: i18n,
      textScaler: textScaler,
      letterSpacing: letterSpacing,
      maxLines: maxLines,
      overflow: overflow,
      underline: underline,
      fontFamily: fontFamily,
    );
  }
}
